package org.utils.mail;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

/**
 * @author  Mektoub
 * @version 1.0
 */
public class MailUtils extends Object
{
	private static Logger logger = Logger.getLogger(MailUtils.class);
	/**
	 * Creates a new MailUtils
	 */
	private MailUtils()
	{
	}

	public static void sendMail(MailInfo info) throws MessagingException
	{
			// create some properties and get the default Session
			Session session = Session.getDefaultInstance(info.getProperties());
			//session.setDebug(true);
			sendMailWithSession(session, info);
	}

	public static void sendMail(MailInfo info, Authenticator auth) throws MessagingException
	{
			// create some properties and get the default Session
			Session session = Session.getDefaultInstance(info.getProperties(), auth);
			//session.setDebug(true);
			sendMailWithSession(session, info);
	}

	public static void sendMailWithSession(Session session, MailInfo info) throws MessagingException
	{
		logger.info("Creating Mail MimeMessage");
	    // create a message
	    Message msg = new MimeMessage(session);

		logger.debug("Setting properties (from, to, ...)");
	    // set the from
	    msg.setFrom(info.getFrom());

		// set the recipients
	    msg.setRecipients(Message.RecipientType.TO, info.getTo());
		// set the copy recipients
	    msg.setRecipients(Message.RecipientType.CC, info.getCc());
		// set the copy recipients
	    msg.setRecipients(Message.RecipientType.BCC, info.getBcc());
	    //set the reply-to
	    msg.setReplyTo(info.getReplyTo());
	    //set the date
	    msg.setSentDate(new Date());
	    // Setting the Subject and Content Type
	    msg.setSubject(info.getSubject());

	    // Optional : You can also set your custom headers in the Email if you Want
//	    msg.addHeader("MyHeaderName", "myHeaderValue");

		logger.debug("Setting content and attachments");
		Multipart content = createMultipartContent(info.getMessage(), info.getContentType());
		// create the multipart content if attachment is detected
		if (info.hasAttachments())
			addFiles(content, info.getAttachments());
		msg.setContent(content);

		logger.info("Sending mail");
	    Transport.send(msg);
	}
	/**
	 * Method createMultipartContent.
	 * @param info
	 */
	public static Multipart createMultipartContent(String message, String contentType) throws MessagingException
	{
		Multipart content = new MimeMultipart();
		// Add the message of the email
		BodyPart bodyPart = new MimeBodyPart();
		bodyPart.setContent(message, contentType);
		content.addBodyPart(bodyPart);
		return content;
	}
	/**
	 * 
	 */
	public static void addFiles(Multipart content, List files) throws MessagingException
	{
		// Add the files
		BodyPart bodyPart = null;
		Iterator iterator = files.iterator();
		while (iterator.hasNext())
		{
			String filename = (String) iterator.next();
			bodyPart = new MimeBodyPart();
			DataSource source = new FileDataSource(filename);
			bodyPart.setDataHandler( new DataHandler(source) );
			bodyPart.setFileName(filename);
			content.addBodyPart(bodyPart);
		}
	}
}
